<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\models\Permission;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class PermissionController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {

        // 接收搜索内容
        $keywords = $request->input('keywords') ? $request->input('keywords') : '';

        // 接收分页页数
        $count = $request->input('count') ? $request->input('count') : '3';

        // 接收所有参数
        $res = $request -> all();
        // 按条件查询信息
        $permission = Permission::where('pname','like','%'.$keywords.'%')->paginate($count);

        return view('admin.permission.index',compact('permission','count','keywords','res'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('admin.permission.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //思路
        //1 接受客户端提交过来的数据
        //2 做表单数据验证
        //3 执行添加操作
        //4 根据结果做逻辑处理，添加成功跳转到列表页，添加失败返回到添加页


        //获取前台表单提交的数据的第一种方法
        $input =  $request->except('_token');


        //记住这个地方返回的是刚添加的那条记录

        $res =  Permission::create($input);


        if($res){

            return redirect('admin/permission');
        }else{
            return back()->with('msg','添加失败');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        // 查取一条信息
        $data = Permission::find($id);
        // dd($data);
        return view('admin.permission.edit',compact('data'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        // 接收表单提交内容
        $pname = $request->input('pname');
        $description = $request->input('description');

        // 查询修改哪条内容
        $data = Permission::find($id);
        // dd($data);
        // 修改内容
        $data->pname = $pname;
        $data->description = $description;
        // 判断是否修改成功
        if($data->save()){
            return redirect('/admin/permission');
        }else{
            return back()->with('msg','修改失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $data = [];
        $res = Permission::find($id)->delete();
        if($res){
            $data=[
                'status'=>0,
                'msg'=>'删除成功'
            ];
        }else{
            $data=[
                'status'=>1,
                'msg'=>'删除失败'
            ];
        }
        return $data;
    }
}
